﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Text;
using System.Web.Services;
using System.Collections;
using Catering.View_Operaction;
using Catering_DBOpertion;

namespace Catering.Catering_Page.yl.Daily
{
    public partial class ERepair : System.Web.UI.Page
    {       
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                txt_date.Attributes.Add("onfocus", "WdatePicker({dateFmt:'yyyy/MM/dd'})");
                txt_date.Text = DateTime.Now.ToString("yyyy/MM/dd");                           
                LoadDrop();
               
                ddl_Mtc.Items.Clear();
                if (Drop_BanZu.Items.Count != 0)
                {
                    DataSet ds = SelContractorByCId("日常设备维护");
                    DataTable dt = ds.Tables[0];
                    
                    if (dt.Rows.Count != 0)
                    {
                        ddl_Mtc.DataSource = dt;
                        ddl_Mtc.DataTextField = "CName";
                        ddl_Mtc.DataValueField = "id";
                        ddl_Mtc.DataBind();
                    }
                }
                LoadEForm();     
            }
        }
        public DataSet SelContractorByCId(string contype)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select id,CName");
            strSql.Append(" FROM Contractor ");
            if (contype.Trim() != "")
            {
                strSql.Append(" where ContraType='" + contype + "'");
            }
            return DbHelperSQL.Query(strSql.ToString());
        }
        protected void LoadDrop()
        {
            string userid = Session["PersonnelID"].ToString();
            int companyid = 0;
            int groupid = 0;
            int classid = 0;
            bool isAdmin = false;
            Bind_Data_to_Controls.GetCompanyByCurrentUser(userid, out companyid, out groupid, out classid, out isAdmin);
            Bind_Data_to_Controls.Bind_Area_ddl(Drop_ZuoYeQu, isAdmin, groupid, companyid, false);
            Bind_Data_to_Controls.Bind_Class_ddl(Drop_ZuoYeQu.SelectedItem, Drop_BanZu, isAdmin, classid, false);

            if (Session["RBGropuID"] != null) this.Drop_ZuoYeQu.SelectedValue = Session["RBGropuID"].ToString();
            if (Session["RBClassID"] != null) this.Drop_BanZu.SelectedValue = Session["RBClassID"].ToString();
            if (!isAdmin)
            {
                this.Drop_ZuoYeQu.Enabled = false;
                this.Drop_BanZu.Enabled = false;
            }

            Session.Remove("RBGropuID");
            Session.Remove("RBClassID");
            Session.Remove("RBDate");            
        }
        public void DropBind(DropDownList drop, DataTable dt, string strValue, string strText)
        {
            drop.Items.Clear();
            if (dt != null && dt.Rows.Count > 0)
            {
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    ListItem item = new ListItem();
                    item.Text = dt.Rows[i][strText].ToString();
                    item.Value = dt.Rows[i][strValue].ToString();
                    drop.Items.Add(item);
                }
            }
        }
        protected void Drop_ZuoYeQu_SelectedIndexChanged(object sender, EventArgs e)
        {
            DataTable dt_BanZu = new Catering_Rules.ClassInfo().GetList("AreaID=" + this.Drop_ZuoYeQu.SelectedValue).Tables[0];
            this.DropBind(this.Drop_BanZu, dt_BanZu, "id", "CName");
            ddl_Mtc.Items.Clear();
            if (Drop_BanZu.Items.Count != 0)
            {
                DataSet ds = SelContractorByCId("日常设备维护");
                DataTable dt = ds.Tables[0];
                if (dt.Rows.Count != 0)
                {
                    ddl_Mtc.DataSource = dt;
                    ddl_Mtc.DataTextField = "CName";
                    ddl_Mtc.DataValueField = "id";
                    ddl_Mtc.DataBind();
                }
            }
            LoadEForm();
        }
        protected void Drop_BanZu_SelectedIndexChanged(object sender, EventArgs e)
        {
            //ddl_Mtc.Items.Clear();
            //if (Drop_BanZu.Items.Count != 0)
            //{
            //    DataSet ds = SelContractorByCId("ContraType='日常设备维护' and ClassId ='" + Drop_BanZu.Text + "'");
            //    DataTable dt = ds.Tables[0];
            //    if (dt.Rows.Count != 0)
            //    {
            //        ddl_Mtc.DataSource = dt;
            //        ddl_Mtc.DataTextField = "CName";
            //        ddl_Mtc.DataValueField = "id";
            //        ddl_Mtc.DataBind();
            //    }
            //}
            LoadEForm();
        }
        /// <summary>
        /// 加载故障报修表
        /// </summary>
        /// <param name="classid"></param>
        /// <param name="classname"></param>
        public void LoadEForm()
        {
            try
            {
                DataTable dt_EPlan = new Catering_Rules.EMASppPlanE().GetList("ClassID ='" + Drop_BanZu.SelectedValue + "' and ContractorID='"+ddl_Mtc.SelectedValue+"'").Tables[0];
                dt_EPlan.Columns.Add("tempId");

                for (int i = 0; i < dt_EPlan.Rows.Count; i++)
                {
                    dt_EPlan.Rows[i]["tempId"] = i + 1;
                }
                this.Rep_EPlan.DataSource = dt_EPlan;
                this.Rep_EPlan.DataBind();
            }
            catch
            {

            }
        }
        //添加
        [WebMethod]
        public static string EAdd(string EquipCName, string EquipModel, string BrandArea, string ConDesc, string UserCName, string Drop_ZuoYeQu, string Drop_BanZu, string ddl_Mtc, string txt_date)
        {
            Catering_Model.EMASppPlanE _emasppPlan = new Catering_Model.EMASppPlanE();
            _emasppPlan.DailyManagementID = int.Parse("1");
            _emasppPlan.GroupID = Drop_ZuoYeQu;
            _emasppPlan.ClassID = Drop_BanZu;
            _emasppPlan.EquipCName = EquipCName;
            _emasppPlan.EquipModel = EquipModel;
            _emasppPlan.BrandArea = BrandArea;
            _emasppPlan.ConDesc = ConDesc;
            _emasppPlan.UserCName = UserCName;
            _emasppPlan.ContractorID = ddl_Mtc;
            _emasppPlan.AddDate = Convert.ToDateTime(txt_date);
            new Catering_Rules.EMASppPlanE().Add(_emasppPlan);
            return "OK";
        }
        [WebMethod]
        public static string EEdit(string _id, string EquipCName, string EquipModel, string BrandArea, string ConDesc, string UserCName, string Drop_ZuoYeQu, string Drop_BanZu, string ddl_Mtc, string txt_date)
        {
            int id = 0;
            int areaid = 0;
            Int32.TryParse(_id, out id);
            if (id <= 0) return "NO";

            Catering_Model.EMASppPlanE _EPE = new Catering_Model.EMASppPlanE();

            _EPE.Id = id;
            _EPE.EquipCName = EquipCName;
            _EPE.EquipModel = EquipModel;
            _EPE.BrandArea = BrandArea;
            _EPE.ConDesc = ConDesc;
            _EPE.UserCName = UserCName;
            new Catering_Rules.EMASppPlanE().Update(_EPE);

            return "OK";
        }
        [WebMethod]
        public static ArrayList EGet(string _id)
        {
            ArrayList al = new ArrayList();
            Catering_Model.EMASppPlanE md = new Catering_Rules.EMASppPlanE().GetModel(Convert.ToInt32(_id));
            al.Add(md.Id);
            al.Add(md.EquipCName);
            al.Add(md.EquipModel);
            al.Add(md.BrandArea);
            al.Add(md.ConDesc);
            al.Add(md.UserCName);
            return al;
        }
        protected void Rep_EPlan_ItemCommand(object source, RepeaterCommandEventArgs e)
        {
            if (e.CommandName == "Edel")
            {
                int delID = Convert.ToInt32(e.CommandArgument);
                if (new Catering_Rules.EMASppPlanE().Delete(delID))
                {
                    this.LoadEForm();
                }
            }
        }

        protected void ddl_Mtc_SelectedIndexChanged(object sender, EventArgs e)
        {
            LoadEForm();
        }
    }
}